package com.jade.zsy.base.facade.impl;

import com.jade.bss.DbException;
import com.jade.bss.base.RoleAction;
import com.jade.bss.base.customer.Customer;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.base.utils.MsgUtil;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.context.ApplicationContextUtils;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.util.PagingResult;
import com.jade.zsy.base.common.StatusConstant;
import com.jade.zsy.base.dao.OrgDao;
import com.jade.zsy.base.entity.BusinessCode;
import com.jade.zsy.base.entity.Org;
import com.jade.zsy.base.facade.BusinessCodeFacade;
import com.jade.zsy.base.facade.OrgFacade;
import com.jade.zsy.base.vo.OrgVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * 组织机构
 * User: jinya
 * Date: 2017/5/31
 * Time: 11:08
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
@Service("OrgFacade")
public class OrgFacadeImpl extends BaseFacadeImpl implements OrgFacade {
    private BusinessCodeFacade businessCodeFacade = ApplicationContextUtils.getBean("BusinessCodeFacade");
    @Inject
    @Named("OrgDao")
    private OrgDao entityDao;
    @Override
    @Transactional
    public void save(Object obj) throws DbException {
        Org entity=(Org)obj;
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        if(StringUtils.isEmpty(entity.getCode()))//用户自定义为空系统自动生成
            entity.setCode(businessCodeFacade.nextBusinessCode(BusinessCode.KEY_BASE_ORG));
        entity.setStatus(StatusConstant.RECORD_NORMAL);
        entity.setOwnerId(customer.getOwnerId());
        entity.setCreator(customer.getNickName());
        entity.setCreatorId(customer.getId());
        super.save(entity);
    }
    @Override
    @Transactional
    public void update(Object obj) throws DbException {
        Org entity=(Org)obj;
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        entity.setModifier(customer.getTrueName());
        super.update(entity);
    }

    @Transactional
    public void delete(long[] obj) throws DbException {
        for(long id : obj){
            Org org=entityDao.getById(Org.class,id);
            if(org==null || org.getId()<=0)
                throw new DbException(MsgUtil.RECODRD_NO, MsgUtil.RECODRD_NO_TEXT);
            entityDao.delete(Org.class,org);
        }
    }

    public OrgVo get(long ids) throws Exception {
        Org org=entityDao.getById(Org.class,ids);
        OrgVo vo = CopyBean.copy(org, OrgVo.class);
        if(org!=null && org.getId()>0)
            vo.setParentDesc(org.getName());
        return vo;
    }


    @Transactional
    public void disabled(long[] obj) throws DbException {
      for(long id : obj){
          Org org=entityDao.getById(Org.class,id);
          if(org==null || org.getId()<=0)
              throw new DbException(MsgUtil.RECODRD_NO, MsgUtil.RECODRD_NO_TEXT);
          org.setStatus(StatusConstant.RECORD_STOP);
          entityDao.update(org);
      }
    }

    @Transactional
    public void enabled(long[] obj) throws DbException {
        for(long id : obj){
            Org enOrg=entityDao.getById(Org.class,id);
            if(enOrg==null || enOrg.getId()<=0)
                throw new DbException(MsgUtil.RECODRD_NO, MsgUtil.RECODRD_NO_TEXT);
            enOrg.setStatus(StatusConstant.RECORD_NORMAL);
            entityDao.update(enOrg);
        }
    }

    @Override
    public PagingResult queryEntity(Object obj, int page, int size, String keyword, String... orders) throws DbException,IllegalAccessException, InstantiationException {
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        OrgVo entity=(OrgVo)obj;

        Criteria criteria = new Criteria((page - 1) * size, size);
        if(size==0) criteria = new Criteria();//如果size=0不需要分页 查询全部
        criteria.add(Restrictions.eq("ownerId",customer.getOwnerId()));
        if(StringUtils.isNotEmpty(keyword)){
            criteria.add(Restrictions.or(Restrictions.like("code", keyword),
                    Restrictions.like("name", keyword)));
        }
        if(entity.getStatus()>0){
            criteria.add(Restrictions.eq("status", entity.getStatus()));
        }
        if(StringUtils.isNotEmpty(entity.getName())){
            criteria.add(Restrictions.like("name", entity.getName()));
        }
        if(entity.getParentId()>0){
            criteria.add(Restrictions.eq("parentId", entity.getParentId()));
        }
        if(StringUtils.isNotEmpty(entity.getCode())){
            criteria.add(Restrictions.like("code", entity.getCode()));
        }
        if(StringUtils.isNotEmpty(entity.getPrincipalName())){
            criteria.add(Restrictions.like("principalName", entity.getPrincipalName()));
        }
        if(StringUtils.isNotEmpty(entity.getPrincipalFax())){
            criteria.add(Restrictions.like("principalFax", entity.getPrincipalFax()));
        }
        if(StringUtils.isNotEmpty(entity.getPrincipalEmail())){
            criteria.add(Restrictions.like("principalEmail", entity.getPrincipalEmail()));
        }
        criteria.addOrder(com.jade.framework.base.criterion.Order.desc(RoleAction.SORT_ITEM));
        PagingList pagingList = entityDao.queryEntity(Org.class, criteria);
        PagingResult  result = PagingResult.getResult(CopyBean.copy(pagingList, OrgVo.class), pagingList.getTotalCount(), page, size);
        if(result!=null && result.getRows().length>0){
            for(Object vo: result.getRows()){
              Org org= entityDao.getById(Org.class,((OrgVo)vo).getId());
              if(org!=null && org.getId()>0)
                  ((OrgVo)vo).setParentDesc(org.getName());
            }
        }
        return result;
    }
}
